package com.k1998.mybatis.controller;

import com.alibaba.fastjson.JSON;
import com.k1998.mybatis.utils.QueryInfo;
import com.k1998.mybatis.bean.StudentLogin;
import com.k1998.mybatis.dao.IStudentLoginDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = {"学生登录Api文档"})
public class StudentLoginController {
    @Autowired
    private IStudentLoginDao iStudentLoginDao;

    @PostMapping("/allStudentLogin")
    public String getAll(@RequestBody QueryInfo queryInfo) {
        // 查询到的结果数量
        int numbers = iStudentLoginDao.getCounts("%"+queryInfo.getQuery()+"%");
        // 第几条数据开始
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<StudentLogin> students = iStudentLoginDao.findAll("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",students);
        res.put("code","success");
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @PostMapping("/studentLogin")
    public String studentLogin(@RequestBody StudentLogin studentLogin) {
        String str = "error";
        int count = iStudentLoginDao.getStudentByLogin(studentLogin.getUsername(),studentLogin.getPassword());
        if (count > 0) {
            str = "ok";
        }
        return str;
    }


    @PostMapping("/detailStudentLogin")
    public String detailStudentLogin(@RequestBody StudentLogin studentLogin){
        StudentLogin s=iStudentLoginDao.detailStudentLogin(studentLogin);
        HashMap<String, Object> res = new HashMap<>();
        res.put("data",s);
        res.put("code","success");
        String users_json = JSON.toJSONString(res);
        return users_json;
    }

    @PostMapping("/insertStudentLogin")
    @ApiOperation(value = "添加学生个人信息", notes="添加学生个人信息", httpMethod = "POST")
    public String insertStudentLogin(@RequestBody StudentLogin studentLogin){
        int i = iStudentLoginDao.insertStudentLogin(studentLogin);
        String str = i >0?"success":"error";
        return str;
    }

    @PostMapping("/deleteStudentLogin")
    @ApiOperation(value = "删除学生个人信息", notes="删除学生个人信息", httpMethod = "POST")
    public String deleteStudentLogin(@RequestBody StudentLogin studentLogin){

        int i = iStudentLoginDao.deleteStudentLogin(studentLogin);
        String str = i >0?"success":"error";
        return str;
    }
    @PostMapping("/updateStudentLogin")
    @ApiOperation(value = "修改学生个人信息", notes="修改学生个人信息", httpMethod = "POST")
    public String updateStudentLogin(@RequestBody StudentLogin studentLogin){
        System.out.println(studentLogin);
        int i = iStudentLoginDao.updateStudentLogin(studentLogin);
        String str = i >0?"success":"error";
        return str;
    }
}
